﻿using UnityEngine;

namespace Global.Tool
{
    /// <summary>
    /// Unity场景单例模式
    /// </summary>
    /// <typeparam name="T">组件类型</typeparam>
    public class UnitySceneSingleton<T> : MonoBehaviour
        where T : Component
    {
        private static T _instance;
        public static T Instance
        {
            get
            {
                if (_instance != null) return _instance;
                _instance = FindObjectOfType<T>();
                if (_instance != null) return _instance;
                var obj = new GameObject
                {
                    hideFlags = HideFlags.HideAndDontSave,
                    name = typeof(T).Name
                };
                //var obj = new GameObject();
                _instance = obj.AddComponent<T>();
                return _instance;
            }
        }
    }
}
